{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c111f500",
   "metadata": {},
   "source": [
    "# 识食家用户需求文档  \n",
    "\n",
    "## 一、产品介绍  \n",
    "\n",
    " **产品名称**  |  **识食家** \n",
    "-|-\n",
    " **产品类型**  | **小程序** \n",
    " **产品描述**  |  **一款帮助美食达人快速识别美味佳肴用料的小程序** \n",
    " \n",
    "\n",
    "### （一）价值主张宣言\n",
    "\n",
    "* 当今时代依旧是民以食为天，但越来越多的人对“美”有了质的追求，希望在享受美食的同时也能保持健康美。许多健身达人、明星们对于自己的身材管理很严格还包括了想要减肥的人，他们对于每次用餐需要摄入多少的卡路里有着“尺”般的严，所以我们采用api的菜品识别技术能够帮助他们快速的只需通过拍照就能知道自己用餐的卡路里。同时，对于大众美食爱好者也可以通过api菜品识别来快速找到做这道美食的方法以及用料。\n",
    "\n",
    "\n",
    "### （二）核心价值\n",
    "\n",
    "1、通过百度ai的菜品识别api技术可以一键拍照轻松上传图片快速识别出菜品的名称以及卡路里，帮助使用者更好的控制每日卡路里摄入。\n",
    "\n",
    "2、给热爱美食并且爱做美食的人群提供一个可以快速找到菜品对应做法的平台。\n",
    "\n",
    "\n",
    "## 二、问题描述与需求\n",
    "\n",
    "\n",
    "### （一）目标用户\n",
    "\n",
    "* 健身达人\n",
    "\n",
    "* 明星（有身材管理需求）  \n",
    "\n",
    "* 减肥人士\n",
    "\n",
    "* 热爱美食的群体（喜欢做美食）\n",
    "\n",
    "\n",
    "### (二) 用户痛点&需求\n",
    "| 目标用户     | 用户问题描述                 | 使用场景 |  需求    |\n",
    "|--------|------------------------|------|-------- |\n",
    "| 健身达人 | 每餐的卡路里需要严格限制，对于每次摄入的食物无法准确知道卡路里    | 健身期间的每次用餐前需要获得本餐的卡路里摄入量   |  知道每次摄入卡路里的多少  |\n",
    "| 热爱美食的人   | 对于一道陌生的美食想要知道如何烹饪以及它的原材料  | 去到陌生的地方面对陌生的美食，尝试后喜欢上了想要知道它的名称和料理方法 | 想要了解自己感兴趣但又陌生的美食名称以及料理方法   |\n",
    "| 减肥人士 | 想要知道自己每餐的卡路里摄入量，如果超过需要消耗掉对应的卡路里  | 在减肥期间每天都需要知道每餐的卡路里摄入量   | 知道每次摄入卡路里的多少  |\n",
    "| 明星（需要管理身材） | 在跑公告以及拍戏期间尤其是女明星想要保持好身材，需要对自己的饮食进行严格把控，需要知道食物热量 | 在用餐吃饭前知道本餐的热量  |  需要知道每次食物的热量  |\n",
    "\n",
    "### （三）用户画像\n",
    "\n",
    "\n",
    "\n",
    "### （四）需求列表&API加值\n",
    "\n",
    "\n",
    "优先级|用户需求|API智能加值|API类型|\n",
    "  ----  | ----  |  ---- |  ---- |\n",
    "1|快速知道菜品名字|是，用户拍照上传后即可知道佳肴名 | 百度AI开放平台——图像识别—菜品识别|\n",
    "2|迅速知道佳肴的卡路里即热量（方面计算）|是，图片上传成功后立马知道了这道佳肴的热量|百度AI开放平台——图像识别—菜品识别|\n",
    "3|知道佳肴的做法以及原料|是，反馈中即可查看佳肴的做法|百度AI开放平台——图像识别—菜品识别|\n",
    "\n",
    "\n",
    "## 三、解决方案：数据流程及关键智能API使用 \n",
    "### （一）解决方案表述  \n",
    "* 产品结构图  \n",
    "\n",
    "* 数据流程图\n",
    "\n",
    "### （二）可行性分析  \n",
    "\n",
    "* 用户可欲性：产品应用设计满足了目标用户的基本需求，解决用户对于菜品信息的模糊认知。而拍照识别食物类型，对于用户来说简单易上手，便于操作，技术要求低。\n",
    "* 技术可行性：市面上类似产品，同样适用菜品信息识别功能对菜品信息进行分析。有前者经验进行借鉴，减少技术开发上的困难。并且从准确度方面来说，百度开放了从大规模数据集、深度学习框架算法，到场景化和个性化AI计算能力的全方面基础设施，百度AI图像技术准确率更是高达95%\n",
    "* 商业可行性：市场上相似的产品较少，市场需求空缺较大，暂未出现行业领头产品。对手竞品的功能单一，综合性功能较少，本产品有竞争优势。并且该产品具有明确的核心价值和使用需求，产品的市场未来需求趋势逐步增长\n",
    "\n",
    "### （三）关键技术API应用  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "349b4294",
   "metadata": {},
   "source": [
    "###  菜品识别\n",
    "* 接口描述：该请求用于菜品识别。即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片的菜品名称、卡路里信息、置信度。  \n",
    "* 请求方法：POST\n",
    "* 请求URL：https://aip.baidubce.com/rest/2.0/image-classify/v2/dish  \n",
    "* 获取参数：access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4bfab3cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.db2e5d3bc8c192c0bbf12b1acf711d48.315360000.1963933155.282335-25863398',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdA5uK0ImNOyJ3D0erxdpldEi71YbnJpte2q7Ja4CzWB07PHy3NZvHbKO3Sinyvhr6xftp08HUrnQjo9jdSLa68DfzVg==',\n",
       " 'access_token': '24.81c4d4d794693784ce39a10b16184113.2592000.1651165155.282335-25863398',\n",
       " 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online',\n",
       " 'session_secret': '2ff86f685ba0c6c0057d0f1b0c324534'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload ={\n",
    "        'grant_type':'client_credentials',  \n",
    "        'client_id':'bYlEQgPn5WGkzTKoMYlzQ0Ux',\n",
    "        'client_secret':'HF3EMNFS00S6mVhZVlBDSS7qfMmmKqjy'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c487e6b8",
   "metadata": {},
   "source": [
    "* 请求参数  （一下为两个例子）  \n",
    "- 例1：直接获得菜名和对应的卡路里  \n",
    "- 例2：间接获得菜品的详细信息（介绍包括原料+做法的链接）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7f220cb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "卡路里：331    名称:厥根粉\n"
     ]
    }
   ],
   "source": [
    "# 例1\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url =  \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('lenmian.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':'24.81c4d4d794693784ce39a10b16184113.2592000.1651165155.282335-25863398',\n",
    "    'image':img,\n",
    "    'baike_num':1,\n",
    "    'top_num':1,\n",
    "    'filter_threshold':0.95,\n",
    "    \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "list = response.json()['result']\n",
    "\n",
    "for i in list:\n",
    "    print('卡路里：'+i['calorie']+'    '+'名称:'+i['name']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a706dc36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result_num': 3,\n",
       " 'result': [{'calorie': '233',\n",
       "   'has_calorie': True,\n",
       "   'name': '香锅牛蛙',\n",
       "   'probability': '0.455515',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%A6%99%E9%94%85%E7%89%9B%E8%9B%99/12996899',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/b8389b504fc2d562ebaaecc8e81190ef76c66c70',\n",
       "    'description': '香锅牛蛙是一道以牛蛙、海带为主要食材制作的家常菜。'}},\n",
       "  {'has_calorie': False,\n",
       "   'name': '馋嘴蛙',\n",
       "   'probability': '0.190232',\n",
       "   'baike_info': {}},\n",
       "  {'calorie': '325',\n",
       "   'has_calorie': True,\n",
       "   'name': '泡椒牛蛙',\n",
       "   'probability': '0.132255',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%B3%A1%E6%A4%92%E7%89%9B%E8%9B%99/3804722',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/8d5494eef01f3a292df5c031c46eab315c6035a8d6b3',\n",
       "    'description': '泡椒牛蛙是一道以牛蛙为主要材料制作的川菜。'}}],\n",
       " 'log_id': 1508866626397387417}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 例2\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('laji.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img,\"top_num\":3,\"baike_num\":3}\n",
    "access_token = '24.81c4d4d794693784ce39a10b16184113.2592000.1651165155.282335-25863398'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38c04384",
   "metadata": {},
   "source": [
    "\n",
    "### （四）用户使用流程\n",
    "1.在微信小程序里搜索“识食家”\n",
    "\n",
    "2.打开“识食家”\n",
    "\n",
    "3.欢迎页后注册/登录\n",
    "\n",
    "4.进入首页\n",
    "\n",
    "5.选择拍照，一键拍照/上传图片\n",
    "\n",
    "6.识别后反馈菜品名称和本菜品的热量（卡路里），包括该佳肴的百科做法  \n",
    "\n",
    "### （五）用户界面流程图\n",
    "\n",
    "## 四、风险与对策  \n",
    "* 风险：菜品识别错误或无法识别菜品\n",
    "* 对策：建议用户重新进行菜品识别，如果还是反馈错误请及时进行用户反馈。  \n",
    "**小程序所涉及的API产品风险以菜品识别API风险为主，存在识别错误和识别信息不完整的问题，对此我们会对小程序的功能进行优化，提高用户体验感，不让用户因结果的错误和等待查询的时间过长而烦躁，我们建议用户重新识别或者引导他们对错误的结果进行反馈。从长期来看，用户上传图片对菜品识别进行查询和用户对结果进行反馈这种双向的互动，既带给用户更佳的使用体验，也在不断的进行机器学习，丰富数据库提高识别结果的准确率。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95190c1e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
